package com.sds.sweng.swat.miplatform.ui.design.models.area.sub;

import java.util.List;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;

import com.sds.sweng.swat.miplatform.ui.design.models.component.MTab;
import com.sds.sweng.swat.miplatform.ui.design.utils.CloneUtils;


public class TabHeaderSubArea extends SubArea {

	public static final int
		GAP = 1,
		LEFT_MARGIN = 2,
		RIGHT_MARGIN = 3;

	public static final String
		TABS = "tabs";

	static {
		descriptors = new IPropertyDescriptor[] {
			new PropertyDescriptor(TABS, "Tabs")
		};
	}

	public TabHeaderSubArea() {

	}

	public TabHeaderSubArea clone() {
		TabHeaderSubArea clone = (TabHeaderSubArea) super.clone();
		clone.setTabs(CloneUtils.clone(getTabs()));

		return clone;
	}

	@JsonIgnore
	public List getTabs() {
		return getChildren();
	}

	@JsonIgnore
	public void setTabs(List<MTab> tabs) {
		removeChildren();

		if (tabs.size() == 1) {
			tabs.get(0).setSelected(true);

		}

		for (int i=0;i<tabs.size();i++) {
			addChild(tabs.get(i));
		}

		firePropertyChange(TABS, null, tabs);
	}

	public Object getPropertyValue(Object id) {
		if (TABS.equals(id))
			return getTabs();

		return null;
	}

	public void setPropertyValue(Object id, Object value) {
		if (TABS.equals(id))
			setTabs((List<MTab>) value);
	}

	@Override
	public boolean isEmpty() {
		if (getTabs().size() > 0)
			return false;

		return true;
	}

	@Override
	public int getRemainWidth() {
		int remainWidth = getWidth() - LEFT_MARGIN - RIGHT_MARGIN;

		for (int i=0;i<getTabs().size();i++) {
			MTab tab = (MTab) getTabs().get(i);
			remainWidth -= tab.getWidth();

			if (i < getTabs().size()-1)
				remainWidth -= GAP;
		}

		return remainWidth;
	}

	@Override
	public int getRequireWidth() {
		int requireWidth = LEFT_MARGIN + RIGHT_MARGIN;

		for (int i=0;i<getTabs().size();i++) {
			MTab tab = (MTab) getTabs().get(i);
			requireWidth += tab.getWidth();

			if (i < getTabs().size()-1)
				requireWidth += GAP;
		}

		return requireWidth;
	}

}
